import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opentae.data.mall.beans.MallArea;
import com.opentae.data.mall.beans.MallCity;
import com.opentae.data.mall.beans.MallProv;
import com.opentae.data.mall.examples.MallProvExample;
import com.opentae.data.mall.interfaces.MallAreaMapper;
import com.opentae.data.mall.interfaces.MallCityMapper;
import com.opentae.data.mall.interfaces.MallProvMapper;
import com.shigu.test.BaseSpringTest;
import com.shigu.validation.tags.Telephone;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.util.List;
import java.util.stream.Collectors;

public class ProvData extends BaseSpringTest {
    @Autowired
    MallProvMapper mallProvMapper;
    @Autowired
    MallCityMapper mallCityMapper;
    @Autowired
    MallAreaMapper mallAreaMapper;

    @Test
    public void getJson() throws IOException {
        String jsonpath = this.getClass().getResource("/city.json").getFile();
        File file = new File(jsonpath);
        InputStream is = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        StringBuilder jsonStrs = new StringBuilder();
        JSONArray jsonArray = null;
        String line;
        while ((line = br.readLine()) != null) {
            jsonStrs.append(line);
        }
        br.close();
        is.close();

        jsonArray = JSONArray.parseArray(jsonStrs.toString().trim());


        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject provObj = jsonArray.getJSONObject(i);
            MallProv prov = new MallProv();
            prov.setName(provObj.getString("name"));
            Long provId = Long.valueOf(provObj.getString("code"));
            prov.setProvId(provId);
            String cities = provObj.getString("children");
            JSONArray citiesArray = JSONArray.parseArray(cities);

            mallProvMapper.insertSelective(prov);
            for (int j = 0; j < citiesArray.size(); j++) {
                JSONObject cityObj = citiesArray.getJSONObject(j);
                MallCity city = new MallCity();
                city.setProvId(provId);
                city.setName(cityObj.getString("name"));
                Long cityId = Long.valueOf(cityObj.getString("code"));
                city.setCityId(cityId);
                String counties = cityObj.getString("children");
                JSONArray areas =  JSONArray.parseArray(counties);

                mallCityMapper.insertSelective(city);
                for(int k =0;k<areas.size();k++){
                    JSONObject areaObj = areas.getJSONObject(k);
                    MallArea area = new MallArea();
                    area.setCityId(cityId);
                    area.setName(areaObj.getString("name"));
                    area.setAreaId(Long.valueOf(areaObj.getString("code")));
                    mallAreaMapper.insertSelective(area);
                }
            }
        }
    }

    @Test
    public void ProvJson(){
        MallProvExample example = new MallProvExample();
        List<MallProv> list = mallProvMapper.selectByExample(example);
        List<Provs> json = list.stream().map(o->{
            Provs vo  = new Provs();
            vo.setLabel(o.getName());
            vo.setValue(o.getProvId()+"");
            return vo;
        }).collect(Collectors.toList());

        System.out.println(JSON.toJSONString(json));

    }
}
